package misp_backend.mapper;

import misp_backend.domain.DishDTO;
import misp_backend.domain.Dishes;
import misp_backend.domain.OrderDetailWithOrder;
import misp_backend.domain.Orderdetails;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
* @author yang
* @description 针对表【orderdetails】的数据库操作Mapper
* @createDate 2025-03-19 23:53:25
* @Entity generator.domain.Orderdetails
*/
@Mapper
public interface OrderdetailsMapper extends BaseMapper<Orderdetails> {
    @Select("SELECT od.*, " +
            "o.create_time AS create_time, " +
            "o.table_id, " +
            "d.dish_name, " +
            "d.category_id, " +
            "c.category_name AS category_name " +
            "FROM orderdetails od " +
            "LEFT JOIN restaurant_order o ON od.order_id = o.order_id " +
            "LEFT JOIN dishes d ON od.dish_id = d.dish_id " +
            "LEFT JOIN category c ON d.category_id = c.category_id " +
            "WHERE od.status IN (0,1) " +
            "ORDER BY o.create_time ASC")
    @Results(id = "detailWithOrderMap", value = {
            @Result(column = "orderdetail_id", property = "orderdetailId", id = true),
            @Result(column = "order_id", property = "orderId"),
            @Result(column = "dish_id", property = "dishId"),
            @Result(column = "quantity", property = "quantity"),
            @Result(column = "specs", property = "specs"),
            @Result(column = "description", property = "description"),
            @Result(column = "status", property = "status"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "table_name", property = "tableName"),
            @Result(property = "dish", column = "dish_id",
                    one = @One(select = "findDishWithCategory"))
    })
    List<OrderDetailWithOrder> selectDetailsWithOrders();

    @Select("SELECT d.*, c.category_name AS category_name " +
            "FROM dishes d " +
            "LEFT JOIN category c ON d.category_id = c.category_id " +
            "WHERE d.dish_id = #{dishId}")
    @Results({
            @Result(column = "dish_id", property = "dishId", id = true),
            @Result(column = "dish_name", property = "dishName"),
            @Result(column = "category_id", property = "category.categoryId"),
            @Result(column = "category_name", property = "category.categoryName")
    })
    DishDTO findDishWithCategory(Long dishId);
}




